/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tencent.tendon.convert.jsontest.refer;

import com.tencent.tendon.convert.json.JsonConvert;
import com.tencent.tendon.convert.jsontest.Utility;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author nbzhang
 */
public class Person {

    private String chname;

    private Person parent;

    private List<Person> children;

    private List<Person> brother;

    private List<Person> relations;

    public static void main(String[] args) throws Throwable {

        Person grandpa = new Person();
        grandpa.setChname("爷爷");

        Person father = new Person();
        father.setParent(grandpa);
        father.setChname("父亲");

        Person uncle = new Person();
        uncle.setParent(grandpa);
        uncle.setChname("叔叔");

        Person son = new Person();
        son.setParent(father);
        son.setChname("儿子");

        Person girl = new Person();
        girl.setParent(uncle);
        girl.setChname("堂妹");

        List<Person> brother1 = Arrays.asList(new Person[]{father, uncle});
        List<Person> brother2 = Arrays.asList(new Person[]{son, girl});

        List<Person> relations = Arrays.asList(new Person[]{grandpa, father, uncle, son, girl});

        grandpa.setParent(grandpa);  //测试自身循环
        grandpa.setChildren(Arrays.asList(new Person[]{father, uncle}));
        father.setChildren(Arrays.asList(new Person[]{son}));
        uncle.setChildren(Arrays.asList(new Person[]{girl}));
        grandpa.setRelations(relations);
        father.setRelations(relations);

        father.setBrother(brother1);
        uncle.setBrother(brother1);
        son.setBrother(brother2);
        girl.setBrother(brother2);

        String json = grandpa.toString();
        String convertstr = JsonConvert.convertTo(grandpa);
        System.out.println(json);
        System.out.println(convertstr);
        Person newgrandpa = JsonConvert.convertFrom(json, Person.class);
        System.out.println(newgrandpa == newgrandpa.children.get(0).parent);
        System.out.println(newgrandpa.children.get(1).parent == newgrandpa.children.get(0).parent);
        System.out.println(json.equals(convertstr));
        System.out.println(json.equals(newgrandpa.toString()));

    }

    /**
     * -------setter  getter-----*
     */
    @Override
    public String toString() {
        return Utility.listingString(this);
    }

    public String getChname() {
        return chname;
    }

    public void setChname(String chname) {
        this.chname = chname;
    }

    public Person getParent() {
        return parent;
    }

    public void setParent(Person parent) {
        this.parent = parent;
    }

    public List<Person> getChildren() {
        return children;
    }

    public void setChildren(List<Person> children) {
        this.children = children;
    }

    public List<Person> getBrother() {
        return brother;
    }

    public void setBrother(List<Person> brother) {
        this.brother = brother;
    }

    public List<Person> getRelations() {
        return relations;
    }

    public void setRelations(List<Person> relations) {
        this.relations = relations;
    }
}
